package com.mls.fivedistrict.server.impl;

import com.mls.fivedistrict.common.GDPriceResponse;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.dao.IntegralMapper;
import com.mls.fivedistrict.server.GDPriceService;
import com.mls.fivedistrict.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lixixi on 2018/4/3.
 */

@Service("gdPriceService")
public class GDPriceServiceImpl implements GDPriceService{
    private static final Logger logger=LoggerFactory.getLogger(GDPriceServiceImpl.class);
    @Resource(name="integralMapper")
    IntegralMapper integralMapper;

    @Override
    public ServiceResponse getGDPrice() {
        //获取国盾去价格表
        RestTemplate restTemplate=new RestTemplate();
        String forObject=restTemplate.getForObject("https://www.guodunc.com/api/Five/newprice",String.class);
//        System.out.println(forObject);
        GDPriceResponse gdPriceResponse=JsonUtils.jsonToPojo(forObject, GDPriceResponse.class);
        List<Object> list=gdPriceResponse.getData();
        HashMap<String,String> map=null;
        for(int i=0;i<list.size();i++){
            map= (HashMap<String, String>) list.get(i);
            if(map.get("G_C_ID")!=null&&map.get("G_PRICE")!=null) {
                integralMapper.updatePriceByGDPrice(map.get("G_C_ID"), map.get("G_PRICE"));
            }
        }
        System.out.println("aaaaa");
        return ServiceResponse.createSuccessResponse(gdPriceResponse);
    }
}
